package com.aerozhonghuan.logisticsmall.modules.order.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.modules.order.base.OrderState;
import com.aerozhonghuan.logisticsmall.modules.order.beans.OrdersListBean;
import com.aerozhonghuan.logisticsmall.modules.order.logic.OrderStateControl;
import com.aerozhonghuan.logisticsmall.modules.order.logic.OrderWebRequest;
import com.aerozhonghuan.logisticsmall.widget.CustomDialog;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 我的订单Adapter
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class MyOrdersAdapter extends RecyclerView.Adapter<MyOrdersAdapter.MyViewHolder> {

    private Context context;
    private List<OrdersListBean.OrderListBean> orderList;
    private OnItemClickListener onItemClickListener;

    public MyOrdersAdapter(Context context) {
        this.context = context;
        orderList = new ArrayList<>();
    }

    public void addList(List<OrdersListBean.OrderListBean> orderList) {
        this.orderList.addAll(orderList);
        notifyDataSetChanged();
    }

    public void refreshList(List<OrdersListBean.OrderListBean> orderList) {
        this.orderList.clear();
        this.orderList.addAll(orderList);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_list, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final OrdersListBean.OrderListBean orderListBean = orderList.get(position);
        if (orderListBean == null) {
            return;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(view, orderListBean);
                }
            }
        });
        if (orderListBean.getPlist() != null && orderListBean.getPlist().size() != 0) {
            OrdersListBean.CommodityListBean commodityListBean = orderListBean.getPlist().get(0);
            holder.tvBaseName.setText(commodityListBean.getBaseName());
            holder.tvNumber.setText(String.format("x%s", TextUtils.isEmpty(commodityListBean.getNumber()) ? "--" : commodityListBean.getNumber()));
            holder.tvPrice.setText(String.format("¥ %s", (float) commodityListBean.getPrice() / 100));
        } else {
            holder.tvBaseName.setText("");
            holder.tvNumber.setText("");
            holder.tvPrice.setText("");
        }
        holder.tvStationname.setText(TextUtils.isEmpty(orderListBean.getStationName()) ? "未知" : orderListBean.getStationName());
        holder.tvTotalNumber.setText(String.format("共%s件商品", orderListBean.getTotalNumber()));
        holder.tvTotalPrice.setText(String.valueOf((float) orderListBean.getTotalPrice() / 100));
        final OrderState orderState = OrderStateControl.getOrderStateByCode(orderListBean.getStatus());
        holder.tvStatus.setText(orderState == null ? "未知" : orderState.name);
        if (orderState != null) {
            if (orderState.isHaveActionBtn) {
                holder.btnMore.setText(orderState.actionBtnText);
                holder.relBtn.setVisibility(View.VISIBLE);
                holder.btnMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        orderState.getDialog(context, String.valueOf(orderListBean.getBillId()), new OrderState.OnDialogListener() {
                            @Override
                            public void onSuccess() {
                                orderList.remove(position);
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onFailure() {

                            }
                        }).showDialog();
                    }
                });
            } else {
                holder.relBtn.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tvStationname, tvStatus, tvBaseName, tvPrice, tvNumber, tvTotalPrice, tvTotalNumber;
        public ImageView imgCommodity;
        public RelativeLayout relBtn;
        public Button btnMore;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvStationname = (TextView) itemView.findViewById(R.id.tv_stationname);
            tvStatus = (TextView) itemView.findViewById(R.id.tv_status);
            tvBaseName = (TextView) itemView.findViewById(R.id.tv_basename);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvNumber = (TextView) itemView.findViewById(R.id.tv_number);
            tvTotalPrice = (TextView) itemView.findViewById(R.id.tv_totalprice);
            tvTotalNumber = (TextView) itemView.findViewById(R.id.tv_totalnumber);
            imgCommodity = (ImageView) itemView.findViewById(R.id.img_commodity);
            relBtn = (RelativeLayout) itemView.findViewById(R.id.rel_btn);
            btnMore = (Button) itemView.findViewById(R.id.btn_more);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view, OrdersListBean.OrderListBean orderListBean);
    }

}
